package com.molichuxing.services.infrastructure.service.impl;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.OrderMileageMapper;
import com.molichuxing.services.infrastructure.dto.request.create.OrderMileageCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.OrderPayModifyDto;
import com.molichuxing.services.infrastructure.dto.response.OrderMileageDto;
import com.molichuxing.services.infrastructure.service.OrderMileageService;
import com.molichuxing.services.property.OrderMileagePayStatusEnum;

/**
 * <p>
 * 加购里程订单 服务实现类
 * </p>
 *
 * @author renzhengyu
 * @since 2019-08-12
 */
@Service("orderMileageService")
public class OrderMileageServiceImpl implements OrderMileageService {
	private static final Logger logger = LoggerFactory.getLogger(OrderMileageServiceImpl.class);

	@Resource
	private OrderMileageMapper orderMileageMapper;

	/**
	 * 获取加购里程订单
	 * 
	 * @param masterOrderCode
	 *            主订单号
	 * @return
	 */
	@Override
	public List<OrderMileageDto> getOrderMileageList(Long masterOrderCode) {
		return masterOrderCode == null ? null
				: Convert.toOrderMileageDto(orderMileageMapper.getList(masterOrderCode, null));
	}

	/**
	 * 获取加购里程订单
	 * 
	 * @param masterOrderCode
	 *            主订单号
	 * @param status
	 *            状态
	 * @return
	 */
	@Override
	public List<OrderMileageDto> getOrderMileageList(Long masterOrderCode, OrderMileagePayStatusEnum status) {
		return masterOrderCode == null ? null
				: Convert.toOrderMileageDto(
						orderMileageMapper.getList(masterOrderCode, status == null ? null : status.getValue()));
	}

	/**
	 * 获取有效加购里程订单（待支付、已支付）
	 * 
	 * @param masterOrderCode
	 *            主订单号
	 * @return
	 */
	@Override
	public List<OrderMileageDto> getEffectiveOrderMileageList(Long masterOrderCode) {
		List<Integer> statusList = Arrays.asList(OrderMileagePayStatusEnum.WAIT.getValue(),
				OrderMileagePayStatusEnum.PREPAID.getValue());
		return masterOrderCode == null ? null
				: Convert.toOrderMileageDto(
						orderMileageMapper.getEffectiveList(masterOrderCode, statusList));
	}

	/**
	 * 新增加购里程订单
	 * 
	 * @param orderMileageCreateDto
	 * @return id
	 */
	@Override
	public Integer create(OrderMileageCreateDto createDto) {
		return orderMileageMapper.create(Convert.toOrderMileageBean(createDto));
	}

	/**
	 * 获取加购里程信息
	 * 
	 * @param orderCode
	 *            订单号
	 * @return
	 */
	@Override
	public OrderMileageDto getOrderMileage(Long orderCode) {
		return Convert.toOrderMileageDto(orderMileageMapper.getOrderMileage(orderCode));
	}

	/**
	 * 取消加购订单
	 * 
	 * @param orderCode
	 *            订单号
	 * @return
	 */
	@Override
	public Boolean cancel(Long orderCode) {
		if (orderMileageMapper.cancel(orderCode, OrderMileagePayStatusEnum.WAIT.getValue(),
				OrderMileagePayStatusEnum.CANCELED.getValue()) > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 支付完成修改加购订单信息
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public Boolean modifyPay(OrderPayModifyDto modifyDto) {
		if (orderMileageMapper.modifyPay(Convert.toOrderMileageBean(modifyDto),
				OrderMileagePayStatusEnum.WAIT.getValue()) > 0) {
			return true;
		}
		return false;
	}

}
